<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <title>请稍后，授权登中 ...</title>
    <link rel="stylesheet" href="loading/stylea.css">
</head>

<body>
    <div class="base">
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
    </div>
    <script src="loading/axios.min.js" defer></script>
    <script defer>
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        function qsx(data) {
            var arr = [];
            for (var name in data) {
                arr.push(
                    encodeURIComponent(name) + "=" + encodeURIComponent(data[name])
                );
            }
            arr.push(("v=" + Math.random()).replace(".", ""));
            return arr.join("&");
        }

        function autoLoginRequest() {
            return new Promise((resolve, reject) => {
                axios.defaults.timeout = 60000;
                axios.defaults.baseURL = "";

                var url = "/api/admin/autologin",
                    params = qsx({
                        token: getUrlParam('token'),
                        name: getUrlParam('name'),
                        phone: getUrlParam('phone')
                    });

                axios.post(url, params).then(
                    (response) => {
                        if (Number(response.data.code) == 200) {
                            resolve(response.data.data);
                        } else {
                            reject(response);
                        }
                        // console.log(response);
                    }
                ).catch(
                    (frs) => {
                        reject("登陆授权失败");
                    }
                );
            });
        }

        function userLoginOutOperate() {
            return new Promise((resolve, reject) => {
                var token = getUrlParam('key');

                window.sessionStorage.clear();
				window.localStorage.clear();
                if (token) {
                    axios.post('/api/home/user/logout', {
                        token
                    }).then(
                        (res) => {
                            resolve();
                        }
                    ).catch(
                        (frs) => {
                            reject("退出登陆失败");
                        }
                    );
                } else {
                    resolve();
                }
            });
        }

        window.onload = function () {
            userLoginOutOperate().then(
                autoLoginRequest
            ).then(
                (res) => {
                    window.sessionStorage.setItem('user', JSON.stringify({
                        token: res.authKey,
                        shopMode: Boolean(res.retrospect_model) ? 'all' : 'scan',
                        oda: res.userInfo
                    }));

                    var url = window.location.protocol + "//" + window.location.hostname + ':' + window.location.port,
                        path = window.location.pathname.toLocaleLowerCase(),
                        endNum = path.lastIndexOf("/"),
                        pathName = (endNum == -1) ? path : path.substr(path, endNum);

                    // console.log(url + pathName)

                    // window.location = url + "/user/index.html";
                    window.location = url + "/index.html";
                }
            ).catch(
                (frs) => {
                    console.log('auto-login-fail', frs);
                }
            );
        };
    </script>
</body>

</html>
